package com.sinosoft.tcmcp.system.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.framework.core.api.domain.ApplicationEntity;
import com.sinosoft.framework.core.api.domain.DictEntity;
import com.sinosoft.framework.core.api.domain.GroupEntity;
import com.sinosoft.framework.core.api.domain.IdentityInformationEntity;
import com.sinosoft.framework.core.api.domain.OrganizationEntity;
import com.sinosoft.framework.core.api.domain.ParameterEntity;
import com.sinosoft.framework.core.api.domain.ResourcePermissionEntity;
import com.sinosoft.framework.core.api.domain.RoleUserRefEntity;
import com.sinosoft.framework.core.api.domain.UserEntity;
import com.sinosoft.framework.core.api.service.ApplicationService;
import com.sinosoft.framework.core.api.service.DictService;
import com.sinosoft.framework.core.api.service.IdentityService;
import com.sinosoft.framework.core.api.service.ParameterService;
import com.sinosoft.framework.core.api.service.RoleService;
import com.sinosoft.framework.core.api.service.UserService;
import com.sinosoft.framework.core.common.exception.ObjectIsNotExistedException;
import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinosoft.tcmcp.system.common.SystemContants;
import com.sinosoft.tcmcp.system.security.SecurityUtil;
import com.sinosoft.tcmcp.system.web.dto.AppIdentityData;
import com.sinosoft.tcmcp.system.web.dto.AppUserData;
import com.sinosoft.tcmcp.system.web.dto.ApplicationData;
import com.sinosoft.tcmcp.system.web.dto.DictData;
import com.sinosoft.tcmcp.system.web.dto.GroupData;
import com.sinosoft.tcmcp.system.web.dto.IdentityInfomationData;
import com.sinosoft.tcmcp.system.web.dto.OrganizationData;
import com.sinosoft.tcmcp.system.web.dto.ParameterData;
import com.sinosoft.tcmcp.system.web.dto.ResourcePermissionData;
import com.sinosoft.tcmcp.system.web.dto.RoleUserRefData;
import com.sinosoft.tcmcp.system.web.dto.UserData;

@Controller
@RequestMapping("pms")
public class PmsController{

	@Autowired
	private UserService userService;
	
	@Autowired
    private ApplicationService applicationService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private IdentityService identityService;
	
	@Autowired
	private ParameterService parameterService;
	
	@Autowired
	private DictService dictService;
	
	/**
	 * 获取用户帐号服务
	 * @return	回执数据
	 */
	@RequestMapping(value="getUsernameService", method = RequestMethod.POST)
	@ResponseBody
	public String getLoginName(){
		return SecurityUtil.getUsername();
	}
	
	/**
	 * 获取应用上下文中的用户信息
	 * @param aid
	 * @param username
	 * @return
	 */
	@RequestMapping(value="/getAppUserDataService", method=RequestMethod.POST)
	@ResponseBody
	public ResponseData getUserDataService(String aid, String username){
		if(StringUtil.isBlank(aid) || StringUtil.isBlank(username)) {
			return new ResponseData(false, null, "PmsController:getUserDataService调用失败，参数不能为空", null);
		}
		ApplicationEntity applicationEntity = applicationService.getApplicationById(aid);
		if(applicationEntity == null){
			return new ResponseData(false, null,"应用不存在",null);
		}
		UserEntity userEntity = userService.getUser(aid, username);
		if(userEntity == null){
			return new ResponseData(false, null,"用户信息不在",null);
		}
		List<RoleUserRefEntity> roleUserRefs = userService.getRolesByUser(aid, username);
		if(roleUserRefs == null || roleUserRefs.size() == 0){
			return new ResponseData(false, null,"角色集合信息不存在",null);
		}
		AppUserData appUserData = new AppUserData(getApplicationData(applicationEntity),getUserData(userEntity),getRoleUserRefDatas(roleUserRefs));
		ResponseData response = new ResponseData(true,appUserData);
		return response;
	}
	
	/**
	 * 获取应用上下文中的身份信息
	 * @param iid
	 * @return
	 */
	@RequestMapping(value="/getAppIdentityDataService", method=RequestMethod.POST)
	@ResponseBody
	public ResponseData getAppIdentityData(String iid){
		if(StringUtil.isBlank(iid)) {
			return new ResponseData(false, null,"人员编号不能为空",null);
		}
		IdentityInformationEntity identityInformationEntity = identityService.getIdentityInformationById(iid);
		if(identityInformationEntity==null){
			return new ResponseData(false, null,"该人员身份信息不在",null);
		}
		OrganizationEntity organizationEntity = identityService.getorganizationsByIid(iid);
		if(organizationEntity==null){
			return new ResponseData(false, null,"该机构信息不在",null);
		}
		List<GroupEntity> groupEntityList =  identityService.getgroupsByIid(iid);
		if(groupEntityList.size()<=0){
			return new ResponseData(false, null,"该群组集合信息不在",null);
		}
		AppIdentityData appIdentityData = new AppIdentityData(getIdentityInfomationData(identityInformationEntity),getOrganizationData(organizationEntity),getGroupDatas(groupEntityList));
		ResponseData response = new ResponseData(true,appIdentityData);
		return response;
	}
	
	/**
	 * 获取应用下文中该应用的参数
	 * @param aid
	 * @return
	 */
	@RequestMapping(value="getAppParameterService",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData getAppParameter(String aid){
		if(StringUtil.isBlank(aid)) {
			return new ResponseData(false, null,"参数：应用编号不能为空",null);
		}
		List<ParameterEntity> entity = parameterService.getParametersByAid(aid);
		if(entity==null){
			return new ResponseData(false, null,"该应用参数信息不在",null);
		}else{
			ResponseData result= new ResponseData(true,getParameterDatas(entity));
			return result;
		}
	}
	
	/**
	 * 获取某角色的应用权限信息
	 * @param roleids	角色编号集合
	 * @param aid	应用编号
	 * @return 回执数据
	 */
	@RequestMapping(value="getAppPermissionDataService",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData getAppPermissionDataService(@RequestParam("roleids[]")String[] roleIds,String aid){
		if(StringUtil.isBlank(aid)) {
			return new ResponseData(false, null, "参数：应用编号不能为空", null);
		}
		if(roleIds.length==0) {
			return new ResponseData(false, null, "参数：角色编号集合不能为空", null);
		}
		List<ResourcePermissionEntity> entityList = roleService.getPermissionsByRoles(roleIds, aid);
		if(entityList.size()<=0){
			return new ResponseData(false, null,"该用户的角色为分配该应用的资源权限信息不在",null);
		}else{
			return new ResponseData(true,getResourceDatas(entityList));
		}
	}
	
	 /**
     * 获取应用的所有字典和相应字典项信息
     * @param type
     * @param aid
     * @return 字典表集合信息及其字典项集合信息
     */
    @RequestMapping(value="/getAppDictDataService",method=RequestMethod.POST)
    @ResponseBody
    public ResponseData getAppDictDataService(String type, String aid){
    	if(StringUtil.isBlank(type)) {
			return new ResponseData(false, null,"参数：字典类型不能为空；公用字典为0，业务字典为1",null);
		}
    	if(type.equals("1")){
    		if(StringUtil.isBlank(aid)) {
    			return new ResponseData(false, null,"查询业务字典信息，参数：应用编号不能为空",null);
    		}
    	}
    	List<DictEntity> result = dictService.getDictDictItems(type,aid,null);
    	return new ResponseData(true,getDictsDatas(result));
    }
	
    /**
	 * 用户密码校验
	 * @param password	密码
	 * @return	true/false
	 */
	@RequestMapping(value="passwordCheckService", method = RequestMethod.POST)
	@ResponseBody
	public boolean passwordCheckService(String password){
		try {
			return userService.checkUserPassword(SystemContants.system_aid, SecurityUtil.getUsername(), password);
		} catch (ObjectIsNotExistedException e) {
			return false;
		}
	};
    
	/**
	 * 应用管理数据转换格式单条转换
	 */
	private ApplicationData getApplicationData(ApplicationEntity application){
		ApplicationData applicationData = new ApplicationData();
		applicationData.toApplicationData(application);
	    return applicationData;
	}
	
	/**
	 * 获取用户传输数据
	 * 将实体数据转换为传输数据集合
	 * @param users	用户实体数据集合
	 * @return	用户传输数据集合
	 */
	private UserData getUserData(UserEntity user) {
		UserData userData = new UserData();
		userData.toUserData(user);
		return userData;
	}
	
	/**
	 * 获取角色用户关系传输数据
	 * @param roleUserRefs	角色用户关系实体集合
	 * @return	角色用户关系传输数据集合
	 */
	private List<RoleUserRefData> getRoleUserRefDatas(List<RoleUserRefEntity> roleUserRefs) {
		int size = roleUserRefs.size();
		List<RoleUserRefData> roleUserRefDatas = new ArrayList<RoleUserRefData> ();
		for(int i=0; i<size; i++) {	//	循环将实体数据转换为传输数据
			RoleUserRefData roleUserRefData = new RoleUserRefData();
			roleUserRefData.toRoleUserRefData(roleUserRefs.get(i));
			roleUserRefDatas.add(roleUserRefData);
		}
		return roleUserRefDatas;
	}
	
	/**
	 * 获取应用的权限信息
	 * @param permissionEntitys	权限实体集合
	 * @return	权限传输数据集合
	 */
	private List<ResourcePermissionData> getResourceDatas(List<ResourcePermissionEntity> entitys) {
		int size = entitys.size();
		List<ResourcePermissionData> resourcePermissionDatas = new ArrayList<ResourcePermissionData> ();
		for(int i=0; i<size; i++) {	//	循环将实体数据转换为传输数据
			ResourcePermissionData resourcePermissionData = new ResourcePermissionData();
			resourcePermissionData.toResourcePermissionData(entitys.get(i));
			resourcePermissionDatas.add(resourcePermissionData);
		}
		return resourcePermissionDatas;
	}
	
	/**
	 * 获取参数
	 * @param parameterEntitys	参数实体集合
	 * @return	参数传输数据集合
	 */
	private List<ParameterData> getParameterDatas(List<ParameterEntity> parameterEntitys) {
		int size = parameterEntitys.size();
		List<ParameterData> parameterDatas = new ArrayList<ParameterData> ();
		for(int i=0; i<size; i++) {	//	循环将实体数据转换为传输数据
			ParameterData parameterData = new ParameterData();
			parameterData.toParaneterData(parameterEntitys.get(i));
			parameterDatas.add(parameterData);
		}
		return parameterDatas;
	}
	/**
	 * 获取字典及其字典项
	 * @param parameterEntitys	参数实体集合
	 * @return	参数传输数据集合
	 */
	private List<DictData> getDictsDatas(List<DictEntity> dictEntitys) {
		int size = dictEntitys.size();
		List<DictData> dictDatas = new ArrayList<DictData> ();
		for(int i=0; i<size; i++) {	//	循环将实体数据转换为传输数据
			DictData dictData = new DictData();
			dictData.toDictData(dictEntitys.get(i));
			dictDatas.add(dictData);
		}
		return dictDatas;
	}
	
	/**
	 * 实体类转化成传输类
	 * @param identityInformationEntity
	 * @return
	 */
	private IdentityInfomationData getIdentityInfomationData(IdentityInformationEntity identityInformationEntity){
		IdentityInfomationData identityInfomationData = new IdentityInfomationData();
		identityInfomationData.toIdentityInformationData(identityInformationEntity);
		return identityInfomationData;
	}
	
	/**
	 * 实体类转化成传输类
	 * @param organizationEntity
	 * @return
	 */
	private OrganizationData getOrganizationData(OrganizationEntity organizationEntity){
		OrganizationData organizationData = new OrganizationData();
		organizationData.toOrganizationData(organizationEntity);
		return organizationData;
	}
	
	/**
	 * 群组信息实体类转化成传输类
	 * @param groupEntityList
	 * @return
	 */
	private List<GroupData> getGroupDatas(List<GroupEntity> groupEntityList) {
		int size = groupEntityList.size();
		List<GroupData> groupDataList = new ArrayList<GroupData> ();
		for(int i=0; i<size; i++) {	//	循环将实体数据转换为传输数据
			GroupData groupData = new GroupData();
			groupData.toGroupData(groupEntityList.get(i));
			groupDataList.add(groupData);
		}
		return groupDataList;
	}
	
}
